﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HanGu.Services.Interface;
using HanGu.Entity;

namespace HanGu.Services
{
    public class GroupService : BaseService, IGroup
    {

        public List<Group> GetAll()
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Group.ToList();
            }
        }

        public List<Group> GetAll(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.Group.Where(c => 1 == 1);
                if (!string.IsNullOrEmpty(searchparma.Ttile))
                {
                    query = query.Where(c => c.GroupName == searchparma.Ttile);
                }

                query = base.Paging<Group, Int32>(c => c.ID, query, searchparma.Pagination);

                return query.ToList();
            }
        }
        public List<VMemberGrouped> GetSelected(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.VMemberGrouped.Where(c => 1 == 1);
                if (!string.IsNullOrEmpty(searchparma.Ttile))
                {
                    query = query.Where(c => c.GroupName == searchparma.Ttile);
                }
                if (searchparma.IntObj1.HasValue && searchparma.IntObj1 != 0)
                {
                    query = query.Where(c => c.GroupID == searchparma.IntObj1);
                }
                if (searchparma.IntObj2.HasValue && searchparma.IntObj2 != 0)
                {
                    query = query.Where(c => c.MemberID == searchparma.IntObj2);
                }
                query = base.Paging<VMemberGrouped, string>(c => c.RealName, query, searchparma.Pagination);

                return query.ToList();
            }
        }

        public List<MemberInfo> GetSelecting(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                string sql = "select * from memberinfo where id not in(select memberid from Membergroup where groupid={0})";
                string ids = string.Empty;
                if (searchparma.SubAssignedIn != null && searchparma.SubAssignedIn.Count > 0)
                {
                    var AssignedIns = obj.MemberAssigned.Where(c => searchparma.SubAssignedIn.Contains(c.ManagerID)).Select(c => c.MemberID).ToList<int>();
                    ids = string.Join(",", AssignedIns);
                }
                if (!string.IsNullOrEmpty(ids))
                {
                    sql = sql + " and id in (" + ids + ")";
                }
                var query = obj.ExecuteStoreQuery<MemberInfo>(string.Format(sql, searchparma.IntObj1)).ToList();

                return query.ToList();
            }
        }

        public Group GetByID(int id)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Group.Where(c => c.ID == id).FirstOrDefault();
            }
        }


        public bool Add(Group model)
        {
            try
            {
                this.Add<Group>(model);
                UpdateGroupMember(model);
                return true;
            }
            catch { return false; }
        }
        private bool UpdateGroupMember(Group model)
        {
            try
            {
                using (HaiGuEntities dbConn = new HaiGuEntities())
                {
                    //this.Delete<MemberGroup>(c => c.GroupID == model.ID); 
                    string sql = "delete from MemberGroup where GroupID={0}";
                    dbConn.ExecuteStoreCommand(string.Format(sql, model.ID));
                    if (!string.IsNullOrEmpty(model.MemberID))
                    {
                        int memberid = 0;
                        foreach (var item in model.MemberID.Split(','))
                        {
                            if (int.TryParse(item, out memberid))
                            {
                                MemberGroup mModel = new MemberGroup();
                                mModel.GroupID = model.ID;
                                mModel.MemberID = memberid;
                                dbConn.AddObject(mModel.GetType().Name, mModel);

                            }
                        }

                    }
                    dbConn.SaveChanges();
                }
                return true;
            }
            catch { return false; }
        }
        public bool Update(Group model)
        {
            try
            {
                this.Update<Group>(model);
                UpdateGroupMember(model);
                return true;
            }
            catch { return false; }
        }

        public bool Delete(int id)
        {
            try
            {
                this.Delete<Group>(c => c.ID == id);
                this.Delete<MemberGroup>(c => c.GroupID == id);
                return true;
            }
            catch { return false; }
        }
        public bool DeleteSelected(int memberID, int groupid)
        {
            try
            {
                this.Delete<MemberGroup>(c => c.MemberID == memberID && c.GroupID == groupid);
                return true;
            }
            catch { return false; }
        }


    }
}
